//
//  HRDynamicRootViewController.m
//  Hear
//
//  Created by mac on 2017/6/8.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicRootViewController.h"
#import "HRFollowDynamicViewController.h"
#import "PopMenu.h"
#import "HRPostDynamicViewController.h"
#import "HRRecordAudioViewController.h"
#import "XXPageTabView.h"
#import "XXPageTabItemLable.h"
#import "HRSegmentView.h"
//#import <QPSDK/QPSDK.h>
#import "HRPostVideoDynamicViewController.h"

@interface HRDynamicRootViewController ()<XXPageTabViewDelegate>
@property (nonatomic, strong)HRFollowDynamicViewController *discoverVC;
@property (nonatomic, strong)HRFollowDynamicViewController *hotVC;
@property (nonatomic, strong)HRFollowDynamicViewController *followVC;
@property (nonatomic, strong)PopMenu *popMenu;
@property (nonatomic, strong) XXPageTabView *pageTabView;
@property (nonatomic, strong)HRSegmentView *segmentView;

@end

@implementation HRDynamicRootViewController

//-(HRSegmentView *)segmentView{
//    if (!_segmentView){
//        WeakSelf
//        _segmentView = [[HRSegmentView alloc] initWithFrame:CGRectMake(0, 0, 64*3, 64) andTitleArray:@[@"最新",@"推荐",@"关注"]];
//        _segmentView.cutTypeViewBlock = ^(UITapGestureRecognizer *tap){
//            [weakSelf.pageTabView changeChildControllerOnClick:tap];
//        };
//    }
//    return _segmentView;
//}


-(XXPageTabView *)pageTabView{
    if (!_pageTabView){
        _pageTabView = [[XXPageTabView alloc] initWithChildControllers:self.childViewControllers childTitles:@[@"最新",@"推荐",@"关注"]];
        _pageTabView.selectedTabIndex = 1;
        _pageTabView.frame = self.view.bounds;
        _pageTabView.delegate = self;
        _pageTabView.titleStyle = XXPageTabTitleStyleGradient;
        _pageTabView.indicatorStyle = XXPageTabIndicatorStyleStretch;
        _pageTabView.tabItemFont = CustomUIFont(17);
        _pageTabView.indicatorWidth = 64;
        _pageTabView.unSelectedColor = CustomColor(34, 34, 34, 1);
        _pageTabView.selectedColor = CustomColor(255, 255, 255, 1);
        _pageTabView.tabSize = CGSizeMake(64*3, 64);
        _pageTabView.tabBackgroundColor = NavTinColor;
        _pageTabView.backgroundColor = NavTinColor;
        _pageTabView.bodyBounces = YES;
        _pageTabView.minScale = 0.85;
    }
    return _pageTabView;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}

- (void)setupView{
    
    
    
    
    
    HRFollowDynamicViewController *discoverVC = [HRFollowDynamicViewController new];
    discoverVC.dynamicType = DiscoverDynamic;
    discoverVC.view.frame = CGRectMake(0, 64, kFullWidth, kFullHeight);
    self.discoverVC = discoverVC;

    
    HRFollowDynamicViewController *hotVC = [HRFollowDynamicViewController new];
    hotVC.dynamicType = HotDynamic;;
    hotVC.view.frame = CGRectMake(kFullWidth, 64, kFullWidth, kFullHeight);
    self.hotVC = hotVC;
    
    HRFollowDynamicViewController *followVC = [HRFollowDynamicViewController new];
    followVC.dynamicType = FollowDynaimc;;
    followVC.view.frame = CGRectMake(kFullWidth*2, 64, kFullWidth, kFullHeight);
    self.followVC = followVC;
    
    [self addChildViewController:discoverVC];

    
    [self addChildViewController:hotVC];

    [self addChildViewController:followVC];
    
    
    [self.view addSubview:self.pageTabView];

    
    UIButton *rightButton = [UIButton buttonWithType:0];
    [rightButton setImage:[UIImage imageNamed:@"发帖按钮"] forState:0];
//    rightButton.size = CGSizeMake(40, 40);
    rightButton.titleLabel.font = CustomUIFont(13);
    [rightButton addTarget:self action:@selector(postDynamicClick) forControlEvents:UIControlEventTouchUpInside];
    //    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    //    self.navigationItem.rightBarButtonItem = rightItem;
    rightButton.frame = CGRectMake(kFullWidth-60, 20, 40, 40);
    [self.pageTabView addSubview:rightButton];
    
    //    self.navigationItem.titleView = self.segmentView;
    
    
}


- (void)pageTabViewDidEndChange {
//    DDLog(@"#####%ld", (long)self.pageTabView.selectedTabIndex);
}



- (void)postDynamicClick{
    [self showMenu];
}

- (void)showMenu{
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:3];
    
    MenuItem *menuItem = [MenuItem itemWithTitle:@"图文动态" iconName:@"广场悬浮_心情_触摸_I"];
    [items addObject:menuItem];
    
    menuItem = [MenuItem itemWithTitle:@"语音动态" iconName:@"广场悬浮_语音_触摸_I"];
    [items addObject:menuItem];

    
//    menuItem = [MenuItem itemWithTitle:@"视频动态" iconName:@"广场悬浮_语音_触摸_I"];
//    
//    [items addObject:menuItem];
    
    
    if (!_popMenu) {
        _popMenu = [[PopMenu alloc] initWithFrame:self.view.bounds items:items];
        _popMenu.menuAnimationType = kPopMenuAnimationTypeNetEase;
        _popMenu.perRowItemCount = 2;
    }
    if (_popMenu.isShowed) {
        return;
    }
    WeakSelf
    _popMenu.didSelectedItemCompletion = ^(MenuItem *selectedItem) {
        if ([selectedItem.title isEqualToString:@"图文动态"]){
            HRPostDynamicViewController *postDynamicVC = [HRPostDynamicViewController new];
            postDynamicVC.clickReloadBlock = ^(){
//                [weakSelf getFollowDynamic:YES];
            };
            
            [weakSelf.navigationController pushViewController:postDynamicVC animated:YES];
        }else if ([selectedItem.title isEqualToString:@"语音动态"]){
            HRRecordAudioViewController *audioVC = [HRRecordAudioViewController new];
            
            [weakSelf.navigationController pushViewController:audioVC animated:YES];
        }else if ([selectedItem.title isEqualToString:@"视频动态"]){
//            QupaiSDK *sdk = [QupaiSDK shared];
//            [sdk setDelegte:(id<QupaiSDKDelegate>)self];
//            sdk.enableMoreMusic = NO;
//            sdk.thumbnailCompressionQuality = 1;
//            sdk.enableVideoEffect=NO;
//            /* 基本设置 */
//            UIViewController *recordController = [sdk createRecordViewControllerWithMinDuration:1
//                                                                                    maxDuration:300
//                                                                                        bitRate:2000*1000];
//            recordController.hidesBottomBarWhenPushed = YES;
//            UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:recordController];
//            navigation.navigationBarHidden = YES;
//            [self presentViewController:navigation animated:YES completion:nil];

        }
    };
    
    [_popMenu showMenuAtView:WINDOW];
}




//- (void)qupaiSDK:(id <QupaiSDKDelegate>)sdk compeleteVideoPath:(NSString *)videoPath thumbnailPath:(NSString*)thumbnailPath
//{
//    WeakSelf
//    if (videoPath && thumbnailPath) {
//        [self dismissViewControllerAnimated:YES completion:^{
//            //            UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil);
//            //
//            //            UIImageWriteToSavedPhotosAlbum([UIImage imageWithContentsOfFile:thumbnailPath], nil, nil, nil);
//            HRPostVideoDynamicViewController *postVC = [HRPostVideoDynamicViewController new];
//            postVC.videoPath = videoPath;
//            postVC.thumbnailPath = thumbnailPath;
//            postVC.hidesBottomBarWhenPushed = YES;
//            [weakSelf.navigationController pushViewController:postVC animated:YES];
//        }];
//        
//    }else{
//        [self dismissViewControllerAnimated:YES completion:nil];
//    }
//}
//

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
